<?php

namespace App\Http\Controllers\Admin\Goods;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Intervention\Image\ImageManager;

class GoodsController extends Controller
{
    //AJAX实现属性显示
    public function ajaxAttribute(Request $request)
    {
        $id = $request -> type_id;
        $data = DB::table('attribute') -> where('type_id', $id) -> get();
        return json_encode($data);
        
    }
    //AJAX实现上架状态的改变
    public function ajaxTop(Request $request)
    {
        $res = DB::table('goods') -> where('id', $request -> id)->first() -> is_on_sale;
        if($res == 1)
        {
            $up = DB::table('goods') -> where('id', $request -> id) -> update(['is_on_sale' => 0]);
            return 0;
        }elseif($res == 0)
        {
            $up = DB::table('goods') -> where('id', $request -> id) -> update(['is_on_sale' => 1]);
            return 1; 
        }
    }
    //Ajax实现商品属性的删除
    public function ajaxDelAttr(Request $request,$id)
    {
        //->whereRaw('FIND_IN_SET(2,sent_mail_ids)')
        $id1 = $request -> id;
        //删除库存量数据
        $res = DB::table('goods_number') -> where('goods_id',$id) 
        -> whereRaw("FIND_IN_SET($id1,goods_attr_id)") -> delete();
        //删除商品属性
        DB::table('goods_attr') -> where('id', $request -> id) -> delete();

    }
	//商品列表页面展示
	public function index(Request $request)
	{

        $data = DB::table('goods as c1') 
                //group_concat(字段)将分组的值拼接在一起  不光只显示一个
                -> select('c1.*','c2.name as cname','c3.type_name as tname',DB::raw("group_concat(c5.name) as ext_cname"))
                -> where('c1.goods_name', 'like', '%' . $request -> name . '%')
                //取主分类名称
                -> leftJoin('category as c2', 'c1.cat_id', '=', 'c2.id') 
                //取类型名称
                -> leftJoin('type as c3', 'c1.type_id', '=', 'c3.id')
                //取出所有子分类id
                -> leftJoin('goods_cat as c4', 'c1.id', '=', 'c4.goods_id')
                //取出所有子分类名称
                -> leftJoin('category as c5', 'c4.cat_id', '=', 'c5.id')
                //将商品分组
                ->groupBy('c1.id')
                ->paginate(3);

        
		return view('admin.goods.list',['title' => '商品列表','data' => $data,'request' => $request -> all()]);
	}
    //商品添加页面展示
    public function add()
    {
        //将所有的商品类型传过去,用下拉框展示
        $type = DB::table('type') -> get();
        $category = DB::table('category')
        -> select('*',DB::raw("concat(path, ',', id) as new_path"))
        -> orderBy('new_path')
        -> get();
        //设置分类之间的关系 层级显示
        foreach($category as $k => $v)
        {
            //获得由此字符串分隔的次数
            $num = substr_count($v -> path, ',');
            //重复某个字符串n次
            $str = str_repeat('|----', $num);
            //将数组中对象的值 拼接好
            $category[$k] -> name =  $str. $v -> name;
        }
    	return view('admin.goods.add',['title' => '商品添加','type' => $type,'category' => $category]);
    }

    //商品添加
    public function insert(Request $request)
    {	
        $this->validate($request, [
            'goods_name' => 'required|unique:goods',
            'goods_price' => 'required',
            'goods_desc' => 'required',
            'series'   => 'required',
        ],[
            'goods_name.required' => '商品名称不能为空',
            'goods_name.unique' => '商品名称重名',
            'goods_price.required' => '商品价格不能为空',
            'goods_desc.required' => '商品描述不能为空',
            'series.required'  => '系列类型不能为空',
        ]);
        //获取扩展分类数组
        $goods_cat = $request -> ext_cat_id;

        //获取商品属性数组
        $data = $request -> attr_value;

        //获取商品数组
        $goods_data = $request -> except('_token','attr_value','ext_cat_id');
        $goods_data['goods_name'] = trim($goods_data['goods_name']);
        $goods_data['addtime'] = time();
        //插入前将促销时间处理好
        if($goods_data['promote_start_time'] != '')
        {
            $goods_data['promote_start_time'] = strtotime($goods_data['promote_start_time']);
            if($goods_data['promote_end_time'] != '')
            {
                $goods_data['promote_end_time'] = strtotime($goods_data['promote_end_time']);
            }
        }
        //插入之前先处理图片
        if($request -> hasFile('logo'))
        {
            if($request -> file('logo') -> isValid())
            {
                //获取后缀名
                $suffix = $request -> file('logo') -> getClientOriginalExtension();
                $logo_name = time().mt_rand(10000, 99999) . '.' . $suffix;
                $move = $request -> file('logo') -> move('./uploads/goods_pic',$logo_name);
                if($move)
                {
                    $goods_data['logo'] = $logo_name; 
                    //创建实例
                    // $manager = new ImageManager();
                    // //处理小图
                    // $sm_logo = $manager->make('./uploads/goods_pic/' . $logo_name)->resize(84, 84);
                    // $goods_data['sm_logo'] = 'sm_' . $logo_name;
                    // $sm_logo->save('./uploads/goods_pic/sm_' . $logo_name);
                    // //处理中图
                    // $mid_logo = $manager->make('./uploads/goods_pic/' . $logo_name)->resize(164, 164);
                    // $goods_data['mid_logo'] = 'mid_' . $logo_name;
                    // $mid_logo->save('./uploads/goods_pic/mid_' . $logo_name);
                    // //处理大图
                    // $big_logo = $manager->make('./uploads/goods_pic/' . $logo_name)->resize(544, 544);
                    // $goods_data['big_logo'] = 'big_' . $logo_name;
                    // $big_logo->save('./uploads/goods_pic/big_' . $logo_name);
                    // //处理更大图
                    // $mbig_logo = $manager->make('./uploads/goods_pic/' . $logo_name)->resize(700, 700);
                    // $goods_data['mbig_logo'] = 'mbig_' . $logo_name;
                    // $mbig_logo->save('./uploads/goods_pic/mbig_' . $logo_name);                                        
                }
            }else
            {
                return back() -> with(['info' => '上传logo失败']);
            }
        }
        //详情图1
        if($request -> hasFile('one_pic'))
        {
            if($request -> file('one_pic') -> isValid())
            {
                //获取后缀名
                $suffix = $request -> file('one_pic') -> getClientOriginalExtension();
                $logo_name = time().mt_rand(10000, 99999) . '.' . $suffix;
                $move = $request -> file('one_pic') -> move('./uploads/goods_pic',$logo_name);
                if($move)
                {
                    $goods_data['one_pic'] = $logo_name;                                     
                }
            }else
            {
                return back() -> with(['info' => '上传详情图1失败']);
            }
        }        
        //详情图2
        if($request -> hasFile('two_pic'))
        {
            if($request -> file('two_pic') -> isValid())
            {
                //获取后缀名
                $suffix = $request -> file('two_pic') -> getClientOriginalExtension();
                $logo_name = time().mt_rand(10000, 99999) . '.' . $suffix;
                $move = $request -> file('two_pic') -> move('./uploads/goods_pic',$logo_name);
                if($move)
                {
                    $goods_data['two_pic'] = $logo_name;                                     
                }
            }else
            {
                return back() -> with(['info' => '上传详情图2失败']);
            }
        }
        //详情图3
        if($request -> hasFile('thr_pic'))
        {
            if($request -> file('thr_pic') -> isValid())
            {
                //获取后缀名
                $suffix = $request -> file('thr_pic') -> getClientOriginalExtension();
                $logo_name = time().mt_rand(10000, 99999) . '.' . $suffix;
                $move = $request -> file('thr_pic') -> move('./uploads/goods_pic',$logo_name);
                if($move)
                {
                    $goods_data['thr_pic'] = $logo_name;                                     
                }
            }else
            {
                return back() -> with(['info' => '上传详情图3失败']);
            }
        }
        //详情图4
        if($request -> hasFile('fou_pic'))
        {
            if($request -> file('fou_pic') -> isValid())
            {
                //获取后缀名
                $suffix = $request -> file('fou_pic') -> getClientOriginalExtension();
                $logo_name = time().mt_rand(10000, 99999) . '.' . $suffix;
                $move = $request -> file('fou_pic') -> move('./uploads/goods_pic',$logo_name);
                if($move)
                {
                    $goods_data['fou_pic'] = $logo_name;                                     
                }
            }else
            {
                return back() -> with(['info' => '上传详情图4失败']);
            }
        }
        //详情图5
        if($request -> hasFile('fiv_pic'))
        {
            if($request -> file('fiv_pic') -> isValid())
            {
                //获取后缀名
                $suffix = $request -> file('fiv_pic') -> getClientOriginalExtension();
                $logo_name = time().mt_rand(10000, 99999) . '.' . $suffix;
                $move = $request -> file('fiv_pic') -> move('./uploads/goods_pic',$logo_name);
                if($move)
                {
                    $goods_data['fiv_pic'] = $logo_name;                                     
                }
            }else
            {
                return back() -> with(['info' => '上传详情图5失败']);
            }
        }
        //详情图6
        if($request -> hasFile('six_pic'))
        {
            if($request -> file('six_pic') -> isValid())
            {
                //获取后缀名
                $suffix = $request -> file('six_pic') -> getClientOriginalExtension();
                $logo_name = time().mt_rand(10000, 99999) . '.' . $suffix;
                $move = $request -> file('six_pic') -> move('./uploads/goods_pic',$logo_name);
                if($move)
                {
                    $goods_data['six_pic'] = $logo_name;                                     
                }
            }else
            {
                return back() -> with(['info' => '上传详情图6失败']);
            }
        }   
        //获取插入数据的id 并执行插入语句
        $id = DB::table('goods') -> insertGetId($goods_data);
        if($id)
        {

            //在扩展分类表中插入数据
            if($goods_cat)
            {
                
                foreach($goods_cat as $v)
                {
                    //定义个空数组接收扩展分类字段
                    if(!empty($v))
                    {
                        $goods_cats=[];
                        $goods_cats['cat_id'] = $v;
                        $goods_cats['goods_id'] = $id;
                        $res_cat = DB::table('goods_cat') -> insert($goods_cats);
                        if(!$res_cat)
                        {
                            return back() -> with(['info' => '添加扩展分类失败']);
                        }
                    }
                }
            }
            //在商品属性表中插入数据
            if($data){
                foreach($data as $k => $v)
                {
                    //把属性值去重
                    $v = array_unique($v);
                    foreach($v as $k1 => $v1)
                    {
                        //拼接数组
                        $goods_attr_data=[];
                        $goods_attr_data['goods_id'] = $id;
                        $goods_attr_data['attr_id']  = $k;
                        $goods_attr_data['attr_value'] = $v1;
                        //插入商品属性表 
                        $res = DB::table('goods_attr') -> insert($goods_attr_data);
                        if(!$res)
                        {
                            return back() -> with(['info' => '添加失败']);
                        }
                    }
                }
            }
            return redirect('admin/goods/list') -> with(['添加成功']);
        }else
        {
            return back() -> with(['info' => '添加失败']);
        }
    }
    public function edit($id)
    {
                //将所有的商品类型传过去,用下拉框展示
        $type = DB::table('type') -> get();
        $category = DB::table('category')
        -> select('*',DB::raw("concat(path, ',', id) as new_path"))
        -> orderBy('new_path')
        -> get();
        //设置分类之间的关系 层级显示
        foreach($category as $k => $v)
        {
            //获得由此字符串分隔的次数
            $num = substr_count($v -> path, ',');
            //重复某个字符串n次
            $str = str_repeat('|----', $num);
            //将数组中对象的值 拼接好
            $category[$k] -> name =  $str. $v -> name;
        }
        //查询该id所指商品的数据
        $data = DB::table('goods') -> where('id', $id) -> first();
        //取出扩展分类的id
        $goods_cat = DB::table('goods_cat') -> select('cat_id') -> where('goods_id', $data -> id) -> get();
        //根据商品类型，取出该类型对应属性表类型将所有属性取出来  然后根据商品的id 和 属性的id 取出该商品的属性值
        // 'c1.id', '=', 'c2.attr_id','and','c2.goods_id','=',$id
        $attribute = DB::table('attribute as c1')
                    -> select('c1.*','c2.attr_value','c2.attr_id','c2.id as goods_attr_id')
                    // -> leftJoin('goods_attr as c2', function($join) use $data
                    // {
                    //     $join -> on('c1.id', '=', 'c2.attr_id') ->on('c2.goods_id','=',$data -> id );
                    // })
                    -> leftJoin('goods_attr as c2','c1.id', '=', 'c2.attr_id')
                    -> where('c2.goods_id','=',$data -> id )
                    -> where('c1.type_id', $data -> type_id)
                    -> get();
        // dd($attribute);
        return view('admin.goods.edit',['title' => '商品编辑','type' => $type,'category' => $category,'data' => $data,'goods_cat' => $goods_cat,'attribute' => $attribute]);
    }

    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'goods_name' => 'required',
            'goods_price' => 'required',
            'goods_desc' => 'required',
        ],[
            'goods_name.required' => '商品名称不能为空',
            'goods_price.required' => '商品价格不能为空',
            'goods_desc.required' => '商品描述不能为空',
        ]);

        $goods_cat = $request -> ext_cat_id;
        //获取商品属性数组
        $data = $request -> attr_value;
        $goods_attr_id = $request -> goods_attr_id;

        //获取商品数组
        $goods_data = $request -> except('_token','attr_value','ext_cat_id','goods_attr_id');
        $goods_data['goods_name'] = trim($goods_data['goods_name']);
        //处理促销时间

        if($goods_data['promote_start_time'] != '')
        {
            $goods_data['promote_start_time'] = strtotime($goods_data['promote_start_time']);
            if($goods_data['promote_end_time'] != '')
            {
                $goods_data['promote_end_time'] = strtotime($goods_data['promote_end_time']);
            }
        }

        //更新之前先处理logo图片
        if($request -> hasFile('logo'))
        {
            if($request -> file('logo') -> isValid())
            {
                //更新图片先将原来的删除
                //先获取该数据 取得其中的图片
                $data1 = DB::table('goods') -> where('id', $id) -> first();
                if($data1 -> logo != '')
                {
                    //将5张图片删除
                    unlink('./uploads/goods_pic/' . $data1 -> logo);
                }

                //获取后缀名
                $suffix = $request -> file('logo') -> getClientOriginalExtension();
                $logo_name = time().mt_rand(10000, 99999) . '.' . $suffix;
                $move = $request -> file('logo') -> move('./uploads/goods_pic',$logo_name);
                if($move)
                {
                    $goods_data['logo'] = $logo_name; 
                    // //创建实例
                    // $manager = new ImageManager();
                    // //处理小图
                    // $sm_logo = $manager->make('./uploads/goods_pic/' . $logo_name)->resize(84, 84);
                    // $goods_data['sm_logo'] = 'sm_' . $logo_name;
                    // $sm_logo->save('./uploads/goods_pic/sm_' . $logo_name);
                    // //处理中图
                    // $mid_logo = $manager->make('./uploads/goods_pic/' . $logo_name)->resize(164, 164);
                    // $goods_data['mid_logo'] = 'mid_' . $logo_name;
                    // $mid_logo->save('./uploads/goods_pic/mid_' . $logo_name);
                    // //处理大图
                    // $big_logo = $manager->make('./uploads/goods_pic/' . $logo_name)->resize(544, 544);
                    // $goods_data['big_logo'] = 'big_' . $logo_name;
                    // $big_logo->save('./uploads/goods_pic/big_' . $logo_name);
                    // //处理更大图
                    // $mbig_logo = $manager->make('./uploads/goods_pic/' . $logo_name)->resize(700, 700);
                    // $goods_data['mbig_logo'] = 'mbig_' . $logo_name;
                    // $mbig_logo->save('./uploads/goods_pic/mbig_' . $logo_name);                                        
                }
            }else
            {
                return back() -> with(['info' => '更新图片失败']);
            }
        }
        //处理详情图1
        if($request -> hasFile('one_pic'))
        {
            if($request -> file('one_pic') -> isValid())
            {
                //更新图片先将原来的删除
                //先获取该数据 取得其中的图片
                $data1 = DB::table('goods') -> where('id', $id) -> first();
                if($data1 -> one_pic != '')
                {
                    //将logo图片删除
                    unlink('./uploads/goods_pic/' . $data1 -> one_pic);
                }

                //获取后缀名
                $suffix = $request -> file('one_pic') -> getClientOriginalExtension();
                $logo_name = time().mt_rand(10000, 99999) . '.' . $suffix;
                $move = $request -> file('one_pic') -> move('./uploads/goods_pic',$logo_name);
                if($move)
                {
                    $goods_data['one_pic'] = $logo_name; 
                                    
                }
            }else
            {
                return back() -> with(['info' => '更新详情1失败']);
            }
        }
        //处理详情图2
        if($request -> hasFile('two_pic'))
        {
            if($request -> file('two_pic') -> isValid())
            {
                //更新图片先将原来的删除
                //先获取该数据 取得其中的图片
                $data1 = DB::table('goods') -> where('id', $id) -> first();
                if($data1 -> two_pic != '')
                {
                    //将logo图片删除
                    unlink('./uploads/goods_pic/' . $data1 -> two_pic);
                }

                //获取后缀名
                $suffix = $request -> file('two_pic') -> getClientOriginalExtension();
                $logo_name = time().mt_rand(10000, 99999) . '.' . $suffix;
                $move = $request -> file('two_pic') -> move('./uploads/goods_pic',$logo_name);
                if($move)
                {
                    $goods_data['two_pic'] = $logo_name; 
                                    
                }
            }else
            {
                return back() -> with(['info' => '更新详情2失败']);
            }
        }
        //处理详情图3
        if($request -> hasFile('thr_pic'))
        {
            if($request -> file('thr_pic') -> isValid())
            {
                //更新图片先将原来的删除
                //先获取该数据 取得其中的图片
                $data1 = DB::table('goods') -> where('id', $id) -> first();
                if($data1 -> thr_pic != '')
                {
                    //将logo图片删除
                    unlink('./uploads/goods_pic/' . $data1 -> thr_pic);
                }

                //获取后缀名
                $suffix = $request -> file('thr_pic') -> getClientOriginalExtension();
                $logo_name = time().mt_rand(10000, 99999) . '.' . $suffix;
                $move = $request -> file('thr_pic') -> move('./uploads/goods_pic',$logo_name);
                if($move)
                {
                    $goods_data['thr_pic'] = $logo_name; 
                                    
                }
            }else
            {
                return back() -> with(['info' => '更新详情3失败']);
            }
        }
        //处理详情图4
        if($request -> hasFile('fou_pic'))
        {
            if($request -> file('fou_pic') -> isValid())
            {
                //更新图片先将原来的删除
                //先获取该数据 取得其中的图片
                $data1 = DB::table('goods') -> where('id', $id) -> first();
                if($data1 -> fou_pic != '')
                {
                    //将logo图片删除
                    unlink('./uploads/goods_pic/' . $data1 -> fou_pic);
                }

                //获取后缀名
                $suffix = $request -> file('fou_pic') -> getClientOriginalExtension();
                $logo_name = time().mt_rand(10000, 99999) . '.' . $suffix;
                $move = $request -> file('fou_pic') -> move('./uploads/goods_pic',$logo_name);
                if($move)
                {
                    $goods_data['fou_pic'] = $logo_name; 
                                    
                }
            }else
            {
                return back() -> with(['info' => '更新详情4失败']);
            }
        }
        //处理详情图5
        if($request -> hasFile('fiv_pic'))
        {
            if($request -> file('fiv_pic') -> isValid())
            {
                //更新图片先将原来的删除
                //先获取该数据 取得其中的图片
                $data1 = DB::table('goods') -> where('id', $id) -> first();
                if($data1 -> fiv_pic != '')
                {
                    //将logo图片删除
                    unlink('./uploads/goods_pic/' . $data1 -> fiv_pic);
                }

                //获取后缀名
                $suffix = $request -> file('fiv_pic') -> getClientOriginalExtension();
                $logo_name = time().mt_rand(10000, 99999) . '.' . $suffix;
                $move = $request -> file('fiv_pic') -> move('./uploads/goods_pic',$logo_name);
                if($move)
                {
                    $goods_data['fiv_pic'] = $logo_name; 
                                    
                }
            }else
            {
                return back() -> with(['info' => '更新详情5失败']);
            }
        }
        //处理详情图6
        if($request -> hasFile('six_pic'))
        {
            if($request -> file('six_pic') -> isValid())
            {
                //更新图片先将原来的删除
                //先获取该数据 取得其中的图片
                $data1 = DB::table('goods') -> where('id', $id) -> first();
                if($data1 -> six_pic != '')
                {
                    //将logo图片删除
                    unlink('./uploads/goods_pic/' . $data1 -> six_pic);
                }

                //获取后缀名
                $suffix = $request -> file('six_pic') -> getClientOriginalExtension();
                $logo_name = time().mt_rand(10000, 99999) . '.' . $suffix;
                $move = $request -> file('six_pic') -> move('./uploads/goods_pic',$logo_name);
                if($move)
                {
                    $goods_data['six_pic'] = $logo_name; 
                                    
                }
            }else
            {
                return back() -> with(['info' => '更新详情6失败']);
            }
        }
        //更新商品数据
        $id1 = DB::table('goods') -> where('id', $id) -> update($goods_data);
        if($id1)
        {

            //在扩展分类表中插入数据
            if($goods_cat)
            {
                //先删除原来的
                $d_goods_cat = DB::table('goods_cat') -> where('goods_id', $id) -> delete();
                if($d_goods_cat)
                {
                    foreach($goods_cat as $v)
                    {
                        //定义个空数组接收扩展分类字段
                        if(!empty($v))
                        {
                            $goods_cats=[];
                            $goods_cats['cat_id'] = $v;
                            $goods_cats['goods_id'] = $id;
                            $res_cat = DB::table('goods_cat') -> insert($goods_cats);
                            if(!$res_cat)
                            {
                                return back() -> with(['info' => '添加扩展分类失败']);
                            }
                        }
                    }                    
                }

            }
            //在商品属性表中更新数据
            // dd($goods_attr_id);
            // if($goods_attr_id)
            // {
                //定义一个变量为循环次数
                $i = 0;
                //循环已存在属性的id
                foreach($data as $k => $v)
                {
                    //把属性值去重
                    $v = array_unique($v);
                    //循环属性值
                    foreach($v as $k1 => $v1 )
                    {
                        //找这个属性值是否有id
                        if($goods_attr_id[$i] == '')
                        {
                            $goods_attr_data=[];
                            $goods_attr_data['goods_id'] = $id;
                            $goods_attr_data['attr_id']  = $k;
                            $goods_attr_data['attr_value'] = $v1;

                            $res = DB::table('goods_attr') -> insert($goods_attr_data);

                        }else
                        {
                            //将字符串转成整型
                            $a = (int)$goods_attr_id[$i];
                            $res1 = DB::table('goods_attr') -> where('id',$a)-> update(['attr_value' => $v1]);

                        }
                        $i++;
                    }
                }
            // }       
            return redirect('admin/goods/list') -> with(['info' =>'更新成功']);
        }else
        {
            return back() -> with(['info' => '更新失败']);
        }               
    }
    //商品的删除
    public function delete($id)
    {
            //删除商品之前 先将扩展分类表删除干净
            $d_goods_cat = DB::table('goods_cat') -> where('goods_id', $id) -> delete();
            //删除商品属性
            DB::table('goods_attr') -> where('goods_id', $id) -> delete();
            //删除库存量
            DB::table('goods_number') -> where('goods_id',$id) -> delete();
            //先获取该数据 取得其中的图片
            $data = DB::table('goods') -> where('id', $id) -> first();
            //将商品删除
            $res = DB::table('goods') -> where('id', $id) -> delete();
            if($res)
            {
                //将logo图片删除
                if($data -> logo != '')
                {
                    
                    unlink('./uploads/goods_pic/' . $data -> logo); 
                }
                //将详情图片1删除
                if($data -> one_pic != '')
                {
                    
                    unlink('./uploads/goods_pic/' . $data -> one_pic); 
                }
                //将详情图片2删除
                if($data -> two_pic != '')
                {
                    
                    unlink('./uploads/goods_pic/' . $data -> two_pic); 
                }
                //将详情图片3删除
                if($data -> thr_pic != '')
                {
                   
                    unlink('./uploads/goods_pic/' . $data -> thr_pic); 
                }
                //将详情图片4删除
                if($data -> fou_pic != '')
                {
                    unlink('./uploads/goods_pic/' . $data -> fou_pic); 
                }
                //将详情图片5删除
                if($data -> fiv_pic != '')
                {
                    unlink('./uploads/goods_pic/' . $data -> fiv_pic); 
                }
                //将详情图片6删除
                if($data -> six_pic != '')
                {
                    unlink('./uploads/goods_pic/' . $data -> six_pic); 
                }               
                return redirect('admin/goods/list') -> with(['info' => '删除成功']);
            }else
            {
                return back() -> with(['info' => '删除失败']);
            }            

    }
    // //商品库存量展示
    // public function show($id)
    // {
    //    //取出商品可选属性
    //    $data = DB::table('goods_attr as c1')
    //             -> select('c1.*','c2.attr_name')
    //             -> leftJoin('attribute as c2', 'c1.attr_id','=','c2.id')
    //             -> where('c1.goods_id',$id)
    //             -> where('c2.attr_type',1) 
    //             -> get();
    //     //处理查询出额数据 二维数组转三维数组
    //     $arr = array();
    //     foreach($data as $k => $v)
    //     {
    //         //属性名称作为下标  名称相同的放在一起
    //         $arr[$v -> attr_name][] = $v;
    //     }
    //     //取出这件商品库存量
    //     $number = DB::table('goods_number') -> where('goods_id', $id) -> get();
        
    //     return view('admin/goods/number',['title'=>'库存量','arr' => $arr,'data' => $data,'number' => $number,'id' => $id]);

       
    // }
    // //商品库存量实现
    // public function number(Request $request,$id)
    // {
    //     //添加之前先删除原来的
    //     DB::table('goods_number') -> where('goods_id',$id) -> delete();
    //    $goods_attr_id = $request -> goods_attr_id;
    //    $goods_number = $request -> goods_number;
    //    //计算比例 
    //    $gaicount = count($goods_attr_id);
    //    $gncount = count($goods_number);
    //    $rate = $gaicount / $gncount;
    //    //从第几个取第几个商品属性id
    //    $a = 0;
    //    //循环库存量
    //    foreach($goods_number as $k => $v)
    //    {
    //         $arr = array();
    //         //从商品属性id数组中取出 $rate个
    //         for($i = 0; $i < $rate; $i++)
    //         {
    //             //将取出来的放到数组中
    //             $arr[] = $goods_attr_id[$a];
    //             $a ++ ; 
    //         }
    //         //升序排列拼接的id 以数字方式
    //         sort($arr,SORT_NUMERIC);
    //         //把取出来的数组转换为字符串
    //         $string = (string)implode(',', $arr);
    //        DB::table('goods_number') -> insert(['goods_id' => $id,'goods_number' => $v,'goods_attr_id' => $string]);
    //    }

    //     return redirect('admin/goods/list') -> with(['info' => '设置成功']);

    // }

}
